package com.tuanbiao.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;

import com.hzjq.data.entity.BlastLonLat;
import com.hzjq.data.hzjq.entity.AppDataEntity;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.MainController;
import com.tuanbiao.databinding.ActivityMainBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.MenuDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.models.MainModel;
import com.tuanbiao.presenter.MainPresenter;
import com.tuanbiao.ui.auth.AuthListActivity;
import com.tuanbiao.ui.auth.AuthLocalActivity;
import com.tuanbiao.ui.auth.AuthOfflineActivity;
import com.tuanbiao.ui.auth.AuthOnlineActivity;
import com.tuanbiao.ui.auth.AuthSequenceActivity;
import com.tuanbiao.ui.blast.BlastNettedActivity;
import com.tuanbiao.ui.plan.DetonatorSingleCheckActivity;
import com.tuanbiao.ui.plan.PlanManageActivity;
import com.tuanbiao.ui.project.ProjectManageActivity;
import com.tuanbiao.ui.setting.SettingActivity;
import com.tuanbiao.ui.upload.UploadDataActivity;
import com.tuanbiao.utils.ClickUtil;

import timber.log.Timber;


/**
 * 首页
 */
public class MainActivity extends BasePresenterActivity<MainPresenter, ActivityMainBinding> implements MainController.View {
    private WaitDialog.Builder checkDialog;
    private int[] models = {R.string.main_auth_online, R.string.main_auth_offline
            , R.string.main_auth_sequence, R.string.main_auth_local, R.string.main_auth_list};

    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(new MainModel(), this);
    }

    @Override
    protected ActivityMainBinding getBindingView() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        getWindow().setFlags(0x80000000,0x80000000);
        binding.llProjectManager.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            ProjectManageActivity.start(getActivity());
        });
        binding.rlLocation.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.startLocation();
        });
        binding.llSingleCheck.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.startSingleCheck();
        });
        binding.llDownloadWorkCode.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.startAuth();
        });
        binding.llDetonatorManage.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.startPlanManage();
        });
        binding.llUploadData.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            UploadDataActivity.start(getActivity());
        });
        binding.llSetting.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingActivity.start(getActivity());
        });
        binding.llBlast.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.startBlast();
        });
        binding.tvBatteryInterval.setOnClickListener(v -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            new MessageDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setMessage("确定要重新计时吗？")
                    .setListener(dialog -> {
                        mPresenter.clearBatteryInterval();
                    }).show();
        });

    }



    @Override
    public void startPlanManage() {
        PlanManageActivity.start(getActivity());
    }

    @Override
    public void startBlast() {
        BlastNettedActivity.start(getActivity());
    }

    @Override
    public void startAuth() {
        // 底部选择框
        new MenuDialog.Builder(this)
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(models)
                .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                    startAuth(position);
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.ANIM_BOTTOM)
                .show();
//        AuthActivity.start(getActivity());
    }

    @Override
    public void startOnlineAuth() {
        AuthOnlineActivity.start(getActivity());
    }

    private void startAuth(int position) {
        switch (position) {
            case 0:
                AuthOnlineActivity.start(getActivity());
                break;
            case 1:
                AuthOfflineActivity.start(getActivity());
                break;
            case 2:
                AuthSequenceActivity.start(getActivity());
                break;
            case 3:
                AuthLocalActivity.start(getActivity());
                break;
            case 4:
                AuthListActivity.start(getActivity());
                break;
            default:
                break;
        }
    }

    @Override
    public void startSingleCheck() {
        DetonatorSingleCheckActivity.start(getActivity());
    }

    @Override
    public void startLocation() {
        LocationActivity.start(getActivity());
    }

    @Override
    public void showLonLat(BlastLonLat blastLonLat) {
        if (!blastLonLat.isAuthRadius()) {
            binding.tvLocationStatus.setText(R.string.main_location_out);
            binding.tvLongitude.setTextColor(getCompatColor(R.color.textDanger));
            binding.tvLatitude.setTextColor(getCompatColor(R.color.textDanger));
            binding.tvLocationStatus.setTextColor(getCompatColor(R.color.textDanger));
        } else {
            binding.tvLocationStatus.setText(R.string.main_location_in);
            binding.tvLongitude.setTextColor(getCompatColor(R.color.textSuccess));
            binding.tvLatitude.setTextColor(getCompatColor(R.color.textSuccess));
            binding.tvLocationStatus.setTextColor(getCompatColor(R.color.textSuccess));
        }
        binding.tvLatitude.setText(getString(R.string.main_location_latitude, blastLonLat.getLat()));
        binding.tvLongitude.setText(getString(R.string.main_location_longitude, blastLonLat.getLon()));
    }

    @Override
    public void showSubTitle(String num) {
        binding.tvToolbarSubTitle.setText(num);
    }

    @Override
    public void showCheckDialog() {
        if (null == checkDialog)
            checkDialog = new WaitDialog.Builder(getActivity()).setMessage("正在检测中");
        checkDialog.show();
    }

    @Override
    public void hideCheckDialog() {
        if (null != checkDialog && checkDialog.isShowing()) {
            checkDialog.dismiss();
        }
    }

    @Override
    public void showUpdateDialog(AppDataEntity entity) {
        new MessageDialog.Builder(getActivity())
                .setTitle("更新提示")
                .setMessage(TextUtils.isEmpty(entity.getRemark()) ? "修复已知Bug" : entity.getRemark())
                .setListener(dialog -> mPresenter.downloadApk(entity)).show();
    }

    @Override
    public void startInstall(Intent installAppIntent) {
        startActivity(installAppIntent);
    }

    @Override
    public void showUploadDialog() {
        new MessageDialog.Builder(getActivity())
                .setTitle("未上传提醒")
                .setMessage("您有未上传的爆破记录，是否现在去上传？")
                .setCancelable(false)
                .setConfirm("现在去")
                .setListener(dialog -> UploadDataActivity.start(getActivity())).show();
    }

    @Override
    public void showBatterInterval(boolean visibility,String startTime) {
        binding.tvBatteryInterval.setVisibility(visibility?View.VISIBLE:View.GONE);
        binding.tvBatteryInterval.setText(startTime);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Timber.i("onKeyDown:" + keyCode);
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}